define(function () {
    var self,userInfos;

    function Fwhf() {
        self = this;
        self.productData = [];
        self.mData = {
            tabType: ['1', '2'],
            orderSet: {}
        };
        self.searchData = {
            date: '',
            type: '2'
        };
        self.currentIndex = 0;
        userInfos = app.userInfos;
        self.init();
    }

    Fwhf.prototype.getData = function (goods) {
        app.ajax({
            xurl: "api/wx/fwhf/getServiceOrderListData",
            // xtime: '1d',
            xdata: {
                page:goods.page +1,
                limit: goods.limit,
                MMRD020: goods.type=='1'?'5':'6',
                GWAK030:userInfos.sysUser.MOBILE,
                MMRDDOCDATE: self.searchData.date
            },
            cb: function (result) {
                if (result.ok) {
                    if (goods.page == 0) {
                        goods.data.removeAll();
                    }
                    for (var i = 0; i < result.data.data.length; i++) {
                        goods.data.push(result.data.data[i]);
                    }

                    var refresh = goods.refresh;
                    if (goods.page == 0) {
                        refresh.endPulldownToRefresh();
                        if (result.data.data.length == 0) {
                            var msg = '';
                            if (goods.type == 1) {
                                msg = '无未回访服务单...';
                            } else if (goods.type == 2) {
                                msg = '无已回访服务单...';
                            }
                            app.showNoData(goods._tip, msg);
                            refresh.disablePullupToRefresh();
                        } else {
                            app.hideNoData(goods._tip);
                            refresh.refresh(true);
                        }
                    }
                    goods.page += 1;
                    refresh.endPullupToRefresh(result.data.data.length < goods.limit);
                }

            }
        });
    };

    Fwhf.prototype.init = function () {

        mui.ready(function () {

            Vue.component('fwhf-listitem', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array
                },
                methods: {
                    open: function (item) {
                        app.openPage({
                            src: "fwhf/fwhf-info.html",
                            argsKey: 'fwhf-info',
                            args: {
                                item: item,
                                cb: function () {
                                    //刷新数据
                                    self.mData.orderSet[self.mData.tabType[self.currentIndex]]
                                        .refresh.pulldownLoading();
                                }
                            }
                        });
                    }
                }
            });

            new Vue({
                el: doc.getElementById('searchs'),
                data: {
                    md: self.searchData
                },
                methods: {
                    today: function () {
                        self.searchData.type = 0;
                        self.searchData.date = new Date().Format('yyyy-MM-dd');
                        var goods = self.mData.orderSet[self.mData.tabType[self.currentIndex]];
                        goods.page = 0;
                        self.getData(goods);
                    },
                    tomorrow: function () {
                        self.searchData.type = 1;
                        self.searchData.date = new Date().addDays(1, 'yyyy-MM-dd');
                        var goods = self.mData.orderSet[self.mData.tabType[self.currentIndex]];
                        goods.page = 0;
                        self.getData(goods);
                    },
                    all: function () {
                        self.searchData.date = '';
                        self.searchData.type = 2;
                        var goods = self.mData.orderSet[self.mData.tabType[self.currentIndex]];
                        goods.page = 0;
                        self.getData(goods);
                    }
                }
            });

            var refreshData = function () {
                var _type = self.mData.tabType[self.currentIndex];
                var goods = self.mData.orderSet[_type];
                if (!goods) {
                    goods = self.mData.orderSet[_type] = {
                        page: 1,
                        limit: 15,
                        data: [],
                        start: function () {
                            return this.page * this.limit;
                        },
                        type: _type
                    };

                    new Vue({
                        el: doc.getElementById('scroll' + _type),
                        data: {
                            items: goods.data
                        }
                    });

                    goods._tip = $('#scroll' + _type);
                    goods.refresh = mui('#scroll' + _type).pullRefresh({
                        down: {
                            callback: function () {
                                goods.page = 0;
                                self.getData(goods);
                            },
                            auto: true
                        }, up: {
                            callback: function () {
                                self.getData(goods);
                            }
                        }
                    });
                }
            };

            refreshData();

            setTimeout(function () {

                doc.getElementById('search_date').addEventListener('change', function () {
                    var goods = self.mData.orderSet[self.mData.tabType[self.currentIndex]];
                    goods.page = 0;
                    self.mData.status = 3;
                    self.getData(goods);
                    return false;
                });

                document.getElementById('slider').addEventListener('slide', function (e) {
                    self.currentIndex = e.detail.slideNumber;
                    refreshData();
                });

            }, 200);
        });
    };

    return new Fwhf();

});